package com.imooc.es.service.impl;

import com.imooc.enums.Type;
import com.imooc.es.mapper.CarouselMapper;
import com.imooc.es.mapper.CategoryMapper;
import com.imooc.es.mapper.CategoryMapperCustom;
import com.imooc.es.pojo.Carousel;
import com.imooc.es.pojo.Category;
import com.imooc.es.pojo.vo.CategoryVO;
import com.imooc.es.pojo.vo.NewItemsVO;
import com.imooc.es.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 顾志杰
 * @Date: 2019/11/21
 * @Time: 14:48
 */
@RestController
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryMapperCustom categoryMapperCustom;
    @Autowired
    private CarouselMapper carouselMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVO> getSubCatList(Integer rootCatId) {
        List<CategoryVO> subCatList = categoryMapperCustom.getSubCatList(rootCatId);

        return subCatList;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) {
        Map map = new HashMap();
        map.put("rootCatId",rootCatId);
        List<NewItemsVO> sixNewItemsLazy = categoryMapperCustom.getSixNewItemsLazy(map);
        return sixNewItemsLazy;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Carousel> queryAll(Integer isShow) {
        Example example = new Example(Carousel.class);
        example.orderBy("sort").desc() ;
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isShow",isShow);
        List<Carousel> carousels = carouselMapper.selectByExample(example);
        return carousels;
    }

    /**
     * 查询一级分类
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryAllRootLecelCat() {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", Type.ONE.type);
        List<Category> categories = categoryMapper.selectByExample(example);
        return categories;
    }
}
